package org.rsbot.script.methods;

import java.awt.Point;
import org.rsbot.client.Client;
import org.rsbot.client.RSGroundData;
import org.rsbot.script.wrappers.RSComponent;
import org.rsbot.script.wrappers.RSLocalPath;
import org.rsbot.script.wrappers.RSPath;
import org.rsbot.script.wrappers.RSPlayer;
import org.rsbot.script.wrappers.RSTile;
import org.rsbot.script.wrappers.RSTilePath;

public class Walking extends MethodProvider
{
  public final int INTERFACE_RUN_ORB = 750;
  private RSPath lastPath;
  private RSTile lastDestination;
  private RSTile lastStep;

  Walking(MethodContext paramMethodContext)
  {
    super(paramMethodContext);
  }

  public RSTilePath newTilePath(RSTile[] paramArrayOfRSTile)
  {
    if (paramArrayOfRSTile == null) {
      throw new IllegalArgumentException("null waypoint list");
    }
    return new RSTilePath(this.methods, paramArrayOfRSTile);
  }

  public RSPath getPath(RSTile paramRSTile)
  {
    return new RSLocalPath(this.methods, paramRSTile);
  }

  public boolean isLocal(RSTile paramRSTile)
  {
    int[][] arrayOfInt = getCollisionFlags(this.methods.game.getPlane());
    int i = paramRSTile.getX() - this.methods.game.getBaseX();
    int j = paramRSTile.getY() - this.methods.game.getBaseY();
    return (arrayOfInt != null) && (i >= 0) && (j >= 0) && (i < arrayOfInt.length) && (j < arrayOfInt.length);
  }

  public boolean walkTo(RSTile paramRSTile)
  {
    if ((paramRSTile.equals(this.lastDestination)) && (this.methods.calc.distanceTo(this.lastStep) < 10))
    {
      return this.lastPath.traverse();
    }
    this.lastDestination = paramRSTile;
    this.lastPath = getPath(paramRSTile);
    if (!this.lastPath.isValid()) {
      return false;
    }
    this.lastStep = this.lastPath.getNext();
    return this.lastPath.traverse();
  }

  public boolean walkTileMM(RSTile paramRSTile)
  {
    return walkTileMM(paramRSTile, 0, 0);
  }

  public boolean walkTileMM(RSTile paramRSTile, int paramInt1, int paramInt2)
  {
    RSTile localRSTile = new RSTile(paramRSTile.getX() + random(0, paramInt1), paramRSTile.getY() + random(0, paramInt2));
    Point localPoint1 = this.methods.calc.tileToMinimap(localRSTile);
    if ((localPoint1.x != -1) && (localPoint1.y != -1)) {
      this.methods.mouse.move(localPoint1);
      Point localPoint2 = this.methods.calc.tileToMinimap(localRSTile);
      if ((localPoint2.x != -1) && (localPoint2.y != -1)) {
        this.methods.mouse.click(localPoint2, true);
        return true;
      }
    }
    return false;
  }

  public boolean walkTileOnScreen(RSTile paramRSTile)
  {
    return this.methods.tiles.doAction(this.methods.calc.getTileOnScreen(paramRSTile), "Walk ");
  }

  public boolean rest()
  {
    return rest(100);
  }

  public boolean rest(int paramInt)
  {
    int i = getEnergy();
    for (int j = 0; j < 5; j++) {
      this.methods.interfaces.getComponent(750, 1).doAction("Rest");
      this.methods.mouse.moveSlightly();
      sleep(random(400, 600));
      int k = this.methods.players.getMyPlayer().getAnimation();
      if ((k == 12108) || (k == 2033) || (k == 2716) || (k == 11786) || (k == 5713)) {
        break;
      }
      if (j == 4) {
        return false;
      }
    }
    while (i < paramInt) {
      sleep(random(250, 500));
      i = getEnergy();
    }
    return true;
  }

  public void setRun(boolean paramBoolean)
  {
    if (isRunEnabled() != paramBoolean)
      this.methods.interfaces.getComponent(750, 0).doClick();
  }

  @Deprecated
  public RSTile[] findPath(RSTile paramRSTile)
  {
    RSLocalPath localRSLocalPath = new RSLocalPath(this.methods, paramRSTile);
    if (localRSLocalPath.isValid()) {
      RSTilePath localRSTilePath = localRSLocalPath.getCurrentTilePath();
      if (localRSTilePath != null) {
        return localRSTilePath.toArray();
      }
    }
    return new RSTile[0];
  }

  @Deprecated
  public RSTile randomize(RSTile paramRSTile, int paramInt1, int paramInt2)
  {
    return paramRSTile.randomize(paramInt1, paramInt2);
  }

  public RSTile getClosestTileOnMap(RSTile paramRSTile)
  {
    if ((!this.methods.calc.tileOnMap(paramRSTile)) && (this.methods.game.isLoggedIn())) {
      RSTile localRSTile1 = this.methods.players.getMyPlayer().getLocation();
      RSTile localRSTile2 = new RSTile((localRSTile1.getX() + paramRSTile.getX()) / 2, (localRSTile1.getY() + paramRSTile.getY()) / 2);
      return this.methods.calc.tileOnMap(localRSTile2) ? localRSTile2 : getClosestTileOnMap(localRSTile2);
    }
    return paramRSTile;
  }

  public boolean isRunEnabled()
  {
    return this.methods.settings.getSetting(173) == 1;
  }

  public int getEnergy()
  {
    try
    {
      return Integer.parseInt(this.methods.interfaces.getComponent(750, 5).getText()); } catch (NumberFormatException localNumberFormatException) {
    }
    return 0;
  }

  public RSTile getDestination()
  {
    if (this.methods.client.getDestX() <= 0) {
      return null;
    }
    return new RSTile(this.methods.client.getDestX() + this.methods.client.getBaseX(), this.methods.client.getDestY() + this.methods.client.getBaseY());
  }

  public int[][] getCollisionFlags(int paramInt)
  {
    return this.methods.client.getRSGroundDataArray()[paramInt].getBlocks();
  }

  public RSTile getCollisionOffset(int paramInt)
  {
    RSGroundData localRSGroundData = this.methods.client.getRSGroundDataArray()[paramInt];
    return new RSTile(localRSGroundData.getX(), localRSGroundData.getY());
  }

  @Deprecated
  public RSTile randomizeTile(RSTile paramRSTile, int paramInt1, int paramInt2)
  {
    return randomize(paramRSTile, paramInt1, paramInt2);
  }

  @Deprecated
  public boolean walkPathMM(RSTile[] paramArrayOfRSTile)
  {
    return walkPathMM(paramArrayOfRSTile, 16);
  }

  @Deprecated
  public boolean walkPathMM(RSTile[] paramArrayOfRSTile, int paramInt)
  {
    return walkPathMM(paramArrayOfRSTile, paramInt, 1, 1);
  }

  @Deprecated
  public boolean walkPathMM(RSTile[] paramArrayOfRSTile, int paramInt1, int paramInt2)
  {
    return walkPathMM(paramArrayOfRSTile, 16, paramInt1, paramInt2);
  }

  @Deprecated
  public boolean walkPathMM(RSTile[] paramArrayOfRSTile, int paramInt1, int paramInt2, int paramInt3)
  {
    try
    {
      RSTile localRSTile = nextTile(paramArrayOfRSTile, paramInt1);
      return (localRSTile != null) && (walkTileMM(localRSTile, paramInt2, paramInt3)); } catch (Exception localException) {
    }
    return false;
  }

  @Deprecated
  public boolean walkPathOnScreen(RSTile[] paramArrayOfRSTile)
  {
    return walkPathOnScreen(paramArrayOfRSTile, 16);
  }

  @Deprecated
  public boolean walkPathOnScreen(RSTile[] paramArrayOfRSTile, int paramInt)
  {
    RSTile localRSTile1 = nextTile(paramArrayOfRSTile, paramInt);
    if (localRSTile1 != null) {
      RSTile localRSTile2 = this.methods.calc.getTileOnScreen(localRSTile1);
      return (localRSTile2 != null) && (this.methods.tiles.doAction(localRSTile2, "Walk"));
    }
    return false;
  }

  @Deprecated
  public RSTile[] reversePath(RSTile[] paramArrayOfRSTile)
  {
    RSTile[] arrayOfRSTile = new RSTile[paramArrayOfRSTile.length];
    for (int i = 0; i < arrayOfRSTile.length; i++) {
      arrayOfRSTile[i] = paramArrayOfRSTile[(paramArrayOfRSTile.length - i - 1)];
    }
    return arrayOfRSTile;
  }

  @Deprecated
  public RSTile nextTile(RSTile[] paramArrayOfRSTile)
  {
    return nextTile(paramArrayOfRSTile, 17);
  }

  @Deprecated
  public RSTile nextTile(RSTile[] paramArrayOfRSTile, int paramInt)
  {
    int i = 99;
    RSTile localRSTile1 = -1;
    for (RSTile localRSTile2 = paramArrayOfRSTile.length - 1; localRSTile2 >= 0; localRSTile2--) {
      localRSTile3 = paramArrayOfRSTile[localRSTile2];
      int j = this.methods.calc.distanceTo(localRSTile3);
      if (j < i) {
        i = j;
        localRSTile1 = localRSTile2;
      }
    }

    localRSTile2 = -1;

    for (RSTile localRSTile3 = localRSTile1; localRSTile3 < paramArrayOfRSTile.length; localRSTile3++)
    {
      if (this.methods.calc.distanceTo(paramArrayOfRSTile[localRSTile3]) > paramInt) break;
      localRSTile2 = localRSTile3;
    }

    if (localRSTile2 == -1) {
      return null;
    }
    return paramArrayOfRSTile[localRSTile2];
  }

  @Deprecated
  public RSTile[] randomizePath(RSTile[] paramArrayOfRSTile, int paramInt1, int paramInt2)
  {
    RSTile[] arrayOfRSTile = new RSTile[paramArrayOfRSTile.length];
    for (int i = 0; i < paramArrayOfRSTile.length; i++) {
      arrayOfRSTile[i] = randomize(paramArrayOfRSTile[i], paramInt1, paramInt2);
    }
    return arrayOfRSTile;
  }
}